package com.zangchat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egzosn.pay.ali.api.AliPayConfigStorage;
import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.wx.api.WxPayConfigStorage;
import com.egzosn.pay.wx.api.WxPayService;
import com.zangchat.config.AliPayDataConfig;
import com.zangchat.config.WxPayDataConfig;
import com.zangchat.domain.ChatAccount;
import com.zangchat.domain.ChatLogThirdpay;
import com.zangchat.service.IChatAccountService;
import com.zangchat.service.IChatLogCashService;
import com.zangchat.service.IChatLogThirdpayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zangchat.mapper.ChatUsersRechargeMapper;
import com.zangchat.domain.ChatUsersRecharge;
import com.zangchat.service.IChatUsersRechargeService;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 用户充值 服务层实现
 *
 * @author wudi
 * @date 2020-07-17
 */
@Service
public class ChatUsersRechargeServiceImpl extends ServiceImpl<ChatUsersRechargeMapper, ChatUsersRecharge> implements IChatUsersRechargeService
{
    @Autowired
    private IChatLogCashService chatLogCashService;
    @Autowired
    private   IChatAccountService chatAccountService;
    @Autowired
    private IChatLogThirdpayService chatLogThirdpayService;
    /**
     * 初始化阿里
     * @return
     */
    @Override
    public AliPayConfigStorage createAliPayConfigStorage() {
        AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage();
        /**吴迪沙箱环境begin**/
        aliPayConfigStorage.setPid(AliPayDataConfig.P_ID);
        aliPayConfigStorage.setAppId(AliPayDataConfig.APP_ID);
        aliPayConfigStorage.setKeyPublic(AliPayDataConfig.PAY_PUBLIC_KEY);
        aliPayConfigStorage.setKeyPrivate(AliPayDataConfig.APP_PRIVATE_KEY);
        aliPayConfigStorage.setNotifyUrl(AliPayDataConfig.NOTIFYURL);
        aliPayConfigStorage.setSignType(AliPayDataConfig.SIGN_TYPE);
        aliPayConfigStorage.setInputCharset(AliPayDataConfig.CHARSET);
        aliPayConfigStorage.setSeller(AliPayDataConfig.SELLER);
        /**吴迪沙箱环境end**/
        //是否为测试账号，沙箱环境
        aliPayConfigStorage.setTest(AliPayDataConfig.IS_TEST);
        return aliPayConfigStorage;
    }

    /**
     * 初始化阿里
     * @return
     */
    @Override
    public PayService initAliPayService() {
        AliPayConfigStorage aliPayConfigStorage = this.createAliPayConfigStorage();
        //支付服务
        PayService service = new AliPayService(aliPayConfigStorage);
        return service;
    }
    @Override
    public WxPayConfigStorage createWxPayConfigStorage() {
        WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
        wxPayConfigStorage.setMchId(WxPayDataConfig.WX_MCH_ID);
        wxPayConfigStorage.setAppid(WxPayDataConfig.WX_APP_ID);
        wxPayConfigStorage.setSecretKey(WxPayDataConfig.API_KEY);
        wxPayConfigStorage.setNotifyUrl(WxPayDataConfig.NOTIFYURL);
        wxPayConfigStorage.setSignType(WxPayDataConfig.SIGN_TYPE);
        wxPayConfigStorage.setInputCharset(WxPayDataConfig.CHARSET);
        return wxPayConfigStorage;
    }
    @Override
    public PayService initWxPayService() {
        WxPayConfigStorage wxPayConfigStorage = createWxPayConfigStorage();
        //支付服务
        PayService service = new WxPayService(wxPayConfigStorage);
        return service;
    }

    @Override
    @Transactional
    public void doNotifyUrl(String outTradeNo,  String totalAmount ,int payType) {
        BigDecimal cash = new BigDecimal(totalAmount);
        //生成充值订单
        ChatUsersRecharge chatUsersRecharge=new ChatUsersRecharge();
        String strUserId = outTradeNo.substring(outTradeNo.indexOf("c"), outTradeNo.indexOf("u"));
        Long userId=Long.valueOf(strUserId);
        chatUsersRecharge.setUserId(userId);
        chatUsersRecharge.setOrderNo(outTradeNo);
        chatUsersRecharge.setPayType(payType);
        chatUsersRecharge.setCash(cash);
        chatUsersRecharge.setStatus(1);
        chatUsersRecharge.setCreateTime(new Date());
        baseMapper.insert(chatUsersRecharge);
        //生成充值流水
        ChatAccount chatAccount=new ChatAccount();
        chatAccount.setUserId(userId);
        QueryWrapper<ChatAccount> queryWrapper1=new QueryWrapper<>(chatAccount);
        ChatAccount chatAccount1 = chatAccountService.getBaseMapper().selectOne(queryWrapper1);
        BigDecimal ordinaryCash = chatAccount1.getOrdinaryCash();
        chatLogCashService.initQlwShopLogCash(1,userId,userId,outTradeNo,ordinaryCash,1,cash,"充值");
        //修改第三方支付记录
        ChatLogThirdpay chatLogThirdpay=new ChatLogThirdpay();
        chatLogThirdpay.setOrderNo(outTradeNo);
        BaseMapper<ChatLogThirdpay> logThirdpayServiceBaseMapper = chatLogThirdpayService.getBaseMapper();
        ChatLogThirdpay logThirdpay = logThirdpayServiceBaseMapper.selectOne(new QueryWrapper<ChatLogThirdpay>(chatLogThirdpay));
        logThirdpay.setHandleStatus(2);
        logThirdpayServiceBaseMapper.updateById(logThirdpay);

    }

    /**
     * 订单
     * @param orderNo 订单号
     * @param cash  充值金额
     * @param payType 支付方式 '充值方式1:线下现金支付2.支付宝3.微信4银联快捷5.第三方支付',
     * @return
     */
    @Override
    public PayOrder initPayOrder(String orderNo,BigDecimal cash, Integer payType) {
        PayOrder payOrder = new PayOrder();

        if (2==payType) {
            payOrder.setPrice(cash);
            payOrder.setTransactionType(AliTransactionType.APP);
        }
        if (3==payType) {
            BigDecimal price = cash.multiply(new BigDecimal(100));
            payOrder.setPrice(price);
        }
        payOrder.setOutTradeNo(orderNo);
        return payOrder;
    }
}